<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="发布标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入发布标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="SEO关键字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoKeywords">
              <a-textarea v-model="model.seoKeywords" rows="4" placeholder="请输入SEO关键字" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="SEO描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoDescription">
              <a-textarea v-model="model.seoDescription" rows="4" placeholder="SEO描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学历" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="education">
              <a-input v-model="model.education" placeholder="请输入学历"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="职位分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag
                type="list"
                v-model="model.categoryId"
                dictCode="ht_position_category,name,id"
                placeholder="请选择职位分类"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="职位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入职位名称"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="职位封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload isMultiple v-model="model.cover"></j-image-upload>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="职位描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="introduction">
              <a-textarea v-model="model.introduction" rows="4" placeholder="请输入职位描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="职位要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" rows="4" placeholder="请输入职位要求" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releaseDate">
              <j-date placeholder="请选择发布日期" v-model="model.releaseDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="截止日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expireDate">
              <j-date placeholder="请选择截止日期" v-model="model.expireDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enterpriseName">
              <a-input v-model="model.enterpriseName" placeholder="请输入公司名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactPerson">
              <a-input v-model="model.contactPerson" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactPhone">
              <a-input v-model="model.contactPhone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <address-cascader ref="addressCascader" />
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item
              label="是否为首页推荐 0-不推荐 1-推荐"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isHomeRecommended"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.isHomeRecommended"
                dictCode="is_home_recommended"
                placeholder="请选择是否为首页推荐 0-不推荐 1-推荐"
              />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item
              label="是否关于我们推荐：0-不推荐，1-推荐"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isAboutRecommended"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.isAboutRecommended"
                dictCode="is_about_recommended"
                placeholder="请选择是否关于我们推荐：0-不推荐，1-推荐"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="状态 0-招聘中，1-已完成，2-已过期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="status"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.status"
                dictCode="is_position_status"
                placeholder="请选择状态 0-招聘中，1-已完成，2-已过期"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import AddressCascader from '@/components/htxincailiao/address/AddressCascader.vue'

export default {
  name: 'HTPositionForm',
  components: {
    AddressCascader
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        // 18 - 湖南
        // 275 - 长沙
        // 2916 - 岳麓区
        provinceId: '18',
        cityId: '275',
        districtId: '2916',
        streetAddress: '航天二路与枫林三路交叉路口向南约50米',
        enterpriseName: '长沙新材料产业研究院有限公司',
        contactPhone: '0731-88146777',
        isHomeRecommended: 0,
        isAboutRecommended: 0,
        status: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        title: [{ required: true, message: '请输入发布标题!' }],
        education: [{ required: true, message: '请输入学历!' }],
        categoryId: [{ required: true, message: '请输入职位分类!' }],
        name: [{ required: true, message: '请输入职位名称!' }],
        introduction: [{ required: true, message: '请输入职位描述!' }],
        content: [{ required: true, message: '请输入职位要求!' }],
        releaseDate: [{ required: true, message: '请输入发布日期!' }],
        // expireDate: [{ required: true, message: '请输入截止日期!' }],
        enterpriseName: [{ required: true, message: '请输入公司名称!' }],
        contactPerson: [{ required: true, message: '请输入联系人!' }],
        contactPhone: [{ required: true, message: '请输入联系电话!' }],
        // provinceId: [{ required: true, message: '请输入省级ID!' }],
        // cityId: [{ required: true, message: '请输入市级ID!' }],
        // districtId: [{ required: true, message: '请输入区ID!' }],
        // streetAddress: [{ required: true, message: '请输入街道地址!' }],
        // isHomeRecommended: [{ required: true, message: '请输入是否为首页推荐 0-不推荐 1-推荐!' }],
        isAboutRecommended: [{ required: true, message: '请输入是否关于我们推荐：0-不推荐，1-推荐!' }],
        status: [{ required: true, message: '请输入状态 0-招聘中，1-已完成，2-已过期!' }]
      },
      url: {
        add: '/position/hTPosition/add',
        edit: '/position/hTPosition/edit',
        queryById: '/position/hTPosition/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.$refs.addressCascader.edit(record)
      this.visible = true
    },
    submitForm() {
      const that = this
      this.$refs.addressCascader
        .submitForm()
        .then(model => {
          if (model) {
            this.$refs.form.validate(valid => {
              if (valid) {
                that.confirmLoading = true
                let httpurl = ''
                let method = ''
                if (!this.model.id) {
                  httpurl += this.url.add
                  method = 'post'
                } else {
                  httpurl += this.url.edit
                  method = 'put'
                }

                const parameters = {
                  ...that.model,
                  // model必须在that.model后面执行，否则addressCascader变更会被覆盖
                  ...model
                }
                console.log('\n\nparameters ' + JSON.stringify(model))
                httpAction(httpurl, parameters, method)
                  .then(res => {
                    if (res.success) {
                      that.$message.success(res.message)
                      that.$emit('ok')
                    } else {
                      that.$message.warning(res.message)
                    }
                  })
                  .finally(() => {
                    that.confirmLoading = false
                  })
              }
            })
          }
        })
        .catch(error => {
          that.$message.error(error.message)
        })
    }
    // submitForm () {
    //   const that = this;
    //   // 触发表单验证
    //   this.$refs.form.validate(valid => {
    //     if (valid) {
    //       that.confirmLoading = true;
    //       let httpurl = '';
    //       let method = '';
    //       if(!this.model.id){
    //         httpurl+=this.url.add;
    //         method = 'post';
    //       }else{
    //         httpurl+=this.url.edit;
    //          method = 'put';
    //       }
    //       httpAction(httpurl,this.model,method).then((res)=>{
    //         if(res.success){
    //           that.$message.success(res.message);
    //           that.$emit('ok');
    //         }else{
    //           that.$message.warning(res.message);
    //         }
    //       }).finally(() => {
    //         that.confirmLoading = false;
    //       })
    //     }
    //
    //   })
    // },
  }
}
</script>

